<!DOCTYPE html>
<html lang="zh">
<head>
    <th:block th:include="include :: header('维度管理')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
    <style>
        .layui-table-click {
            background-color:#1E9FFF;
            color:#fff;
        }
    </style>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="row">
        <div class="col-md-4" style="background-color: #fff;min-height:550px"> <!--style="font-size:16px;margin-top:6px;"-->
            <table id="bootstrap-table" style="margin-top: 10px"></table>
        </div>
        <div class="col-md-8" style="padding-right: 0px;">
            <div class="panel-body" style="background-color: white">
                <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success" onclick="add()">
                        <i class="fa fa-plus"></i> 添加
                    </a>
                </div>
                <table class="table table-bordered" style="margin-top: 10px">
                    <thead id="thead_data" >
                    </thead>
                    <tbody id="tbody_data" >
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<th:block th:include="include :: select2-js" />
<script type="text/javascript">
    var prefix = ctx + "md/med/dim";
    var tableName="";
    function add() {
        if(tableName=="") {
            $.modal.alertWarning("先选择维度");
            return ;
        }
        var options = {
            title:"表["+tableName+"]添加数据",
            width: "700",
            height: "600",
            url: prefix+"/addData/"+tableName,
            callBack: reloadData
        };
        $.modal.openOptions(options);
    }
    function reloadData(index, layero) {
        var iframeWin = layero.find('iframe')[0];
        iframeWin.contentWindow.submitHandler(index, layero);
        tableRefresh();
    }
    function loadDimTable() {
        var na = $("#na").val();
        na = (na==""?null:na);
        $('#bootstrap-table').bootstrapTable({
            showSearch: false,
            showRefresh: false,
            showToggle: false,
            showColumns: false,
            pagination: true,
            pageSize:15,
            search: true,
            sidePagination: 'client',
            showRefresh : true,
            onClickRow: onClickRow,
            data:[{"idPubfld":"ID_DIM_DATETIME_CLASS","cdTbDim":"MD_DIM_DATETIME_CLASS","na":"时间段分类"},
                {"idPubfld":"ID_DIM_DEPT_HIS_MERGE","cdTbDim":"MD_DIM_DEPT_HIS_MERGE","na":"HIS大科室"},
                {"idPubfld":"ID_DIM_DEPT_JDY","cdTbDim":"MD_DIM_DEPT_JDY","na":"金蝶云科室"}],
            columns: [{
                field: 'idPubfld',
                title: '主键',
                visible: false
            },
                {
                    field: 'na',
                    title: '维度名称'
                },
                {
                    field: 'cdTbDim',
                    title: '维度表'
                }]
        });
    }
    var tableRefresh = function () {
        $.ajax({
            url:prefix+"/findDimData/"+tableName,
            cache: false,
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                if(data.code=="0") {
                    var titles = data.data.fldList;
                    var bodyData = data.data.data;
                    var html="<tr><th width='20'></th>";
                    $.each(titles,function(index,item) {
                        html += '<th>'+item.naPubfld+'</th>';
                    });
                    html += '<th>操作</th>';
                    $("#thead_data").html(html+"</tr>");
                    html="";
                    var value;
                    if(bodyData!=null) {
                        for(var i=0;i<bodyData.length;i++) {
                            debugger;
                            html+="<tr><td>"+(i+1)+"</td>";
                            var trData = bodyData[i];
                            for(var j=0;j<titles.length;j++) {
                                value = trData[titles[j].idPubfld];
                                if(value==undefined) {
                                    value = "";
                                }
                                html+="<td>"+value+"</td>";
                            }
                            html+='<td><a class="btn btn-danger btn-xs " href="javascript:void(0)" onclick="remove(\''+trData.cd+'\')"><i class="fa fa-remove"></i>删除</a></td> ';
                            html+="</tr>";
                        }
                        $("#tbody_data").html(html);
                    }
                }
            }
        });
    }
    function onClickRow(row, $element){
        $element.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        var cdTbDim = row.cdTbDim;
        tableName = cdTbDim;
        tableRefresh();
    }
    function remove(cd) {
        $.ajax({
            url:prefix+"/remove?tableName="+tableName+"&cd="+cd,
            cache: false,
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                tableRefresh();
            }
        });
    }
    $(function(){
        loadDimTable();
    });
</script>
</body>
</html>